/**
 * 弹出框
 */
var winDiv;
var winFrame;
var theDataGrid;
var alertPrefix = "<br>&nbsp;&nbsp;";

function initWin() {
	winDiv = $("<div></div>");
	winFrame = $("<iframe frameborder='0' style='width: 100%;height: 100%' id='winFm'/ >");
	winDiv.append(winFrame);
	winDiv.dialog({
		modal : true,
		closed : true
	});
}

function getSelectSomeText(actName,objName){
    return alertPrefix + "请先选择将要" + actName + "的" + objName + "!";
}

function getSelectOneText(actName,objName){
    return alertPrefix + "只能选择一个" + objName + "!";
}

function getConfirmText(actName,objName){
    return alertPrefix + "确认" + actName +"选中的" + objName + "?";
}

/**
 * 弹出窗口
 * @param title
 * @param width
 * @param height
 * @param url
 */
function openUrl(title, width, height, url){
	winDiv.dialog({
		title:title,
	    width:width,  
	    height:height,
	    closed:false
	});
	winFrame.attr("src", url);
}

function closeWin(){
	winDiv.window('close');
}

function selectOneOpenUrl(gridName, url, width, height, actName, objName){
	theDataGrid = $(gridName);
	var rows = theDataGrid.datagrid('getSelections');
    if (rows.length == 0){
        $.messager.alert("提示", getSelectSomeText(actName,objName), "info");
    } else if (rows.length > 1){
            $.messager.alert("提示", getSelectOneText(actName,objName), "info");
    } else {
        var id = rows[0].id;
        if (url.indexOf("?")!=-1){
            url += "&id=" + id + "&t=" + Math.random();
        }else{
            url += "?id=" + id + "&t=" + Math.random();
        }
        openUrl(actName + objName, width, height, url);
    }
}

function checkSelectSomeDoAction(gridName, actName, objName, actionUrl, requireConfirm){
    theDataGrid = $(gridName);
    var rows = $(gridName).datagrid('getSelections');
    if (rows.length == 0){
        $.messager.alert("提示", getSelectSomeText(actName,objName), "info");
    } else {
        var ids = "";
        var arrId = [];
        for (i=0;i<rows.length;i++){
            arrId.push(rows[i].id);
        }
        ids = arrId.join(",");
        if (actionUrl.indexOf("?")!=-1){
        	actionUrl = actionUrl + "&ids=" + ids + "&t=" + Math.random();
        }else{
        	actionUrl = actionUrl + "?ids=" + ids + "&t=" + Math.random();
        }
        if (requireConfirm){
            $.messager.confirm("确认", getConfirmText(actName,objName), function(r) {
                if (r) {
                	doPostAction(actionUrl);
                }
            });
        }else{
        	doPostAction(actionUrl);
        }
    }
}

/**
 * 发送ajax post请求
 * @param actionUrl
 */
function doPostAction(actionUrl){
	$.ajax({
	   type: "POST",
	   url: actionUrl,
	   dataType: "text",
	   success: function(result){
		   if (result == "success") {
	        	theDataGrid.datagrid("reload");
	        } else {
	        	$.messager.alert("提示", alertPrefix + result, 'info');
	        }
	   }
	});
}

function openMaxUrl(title, url){
	var width = $(document).width() - 20;
	var height = $(document).height() - 20;
	openUrl(title, width, height, url);
}

function selectOneOpenMaxUrl(gridName, url, actName, objName){
	var width = $(document).width() - 20;
	var height = $(document).height() - 20;
	selectOneOpenUrl(gridName, url, width, height, actName, objName);
}

/**
 * 页面加载完成初始化弹出框
 */
$(function(){
	initWin();
});
